package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JasonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/travel")
public class TravelController {
    //注入service对象
    @Autowired
    private ITravelService travelService ;


    /**
     * 改变游记的状态，发布和拒绝
     *
     */
    @RequestMapping("/changeState.do")
    @ResponseBody
    public Object changeState(Long id,Integer state){
        JasonResult js = new JasonResult();
        try {
            travelService.changeState(id,state);
        } catch (Exception e) {
            js.mark(e.getMessage());
        }
        return js ;
    }


    /**
     * 仅仅查询游记的文本内容
     */
    @RequestMapping(value = "/conent.do",produces="text/html;charset=utf-8")
    @ResponseBody
    public String getContent(Long id){
        //根据页面发送过来的Id，找到指定的游记的内容
        return travelService.getContent(id).getContent();
        //并返回
    }
    /**
     * 查询所有的游记
     */
    @RequestMapping("/list.do")
    public String list(Model model, @ModelAttribute("qo") TravelQueryObject qo){
        //在这边设置高级查询的条件
        qo.setOrderBy("t.lastUpdateTime");//按照最后的更新时间来排序
        qo.setIsPublic(true);//必须是公开状态的
        //判断，没有设置状态的话就设置为待审核的状态
        if (qo.getState() == null) {
            qo.setState(Travel.STATE_WAIT);
        }
        model.addAttribute("pageInfo",travelService.query(qo));
        return "travel/list" ;
    }
}
